package com.zbkj.front.controller.merchant;

import cn.hutool.core.collection.CollUtil;
import com.zbkj.common.annotation.LogControllerAnnotation;
import com.zbkj.common.enums.MethodType;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.*;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.OrderTraceLogsRecordItemsService;
import com.zbkj.service.service.OrderTraceLogsRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 订单溯源号段表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/merchant/order/trace/logs/record")
@Api(tags = "商户端订单溯源号段") //配合swagger使用

public class OrderTraceLogsRecordController {

    @Autowired
    private OrderTraceLogsRecordService orderTraceLogsRecordService;

    @Autowired
    private OrderTraceLogsRecordItemsService orderTraceLogsRecordItemsService;

    /**
     * 分页显示订单溯源号段表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author Caiyc
     * @since 2025-08-04
     */
    @Ignore
    @ApiOperation(value = "溯源号段列表") //配合swagger使用
    @RequestMapping(value = "/page/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<OrderTraceLogsRecordFrontPageDataResponse>> getList(@Validated OrderTraceLogsRecordFrontSearchRequest request,
                                                                   @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<OrderTraceLogsRecordFrontPageDataResponse> orderTraceLogsRecordCommonPage =
                CommonPage.restPage(orderTraceLogsRecordService.getListH5(request, pageParamRequest));
        return CommonResult.success(orderTraceLogsRecordCommonPage);
    }


    /**
     * 分页显示订单溯源号段明细表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author Caiyc
     * @since 2025-08-04
     */
    @ApiOperation(value = "溯源日志列表") //配合swagger使用
    @RequestMapping(value = "/items/page/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<OrderTraceLogsRecordItemsFrontPageDataResponse>> getList(@Validated OrderTraceLogsRecordItemsFrontSearchRequest request,
                                                                                       @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<OrderTraceLogsRecordItemsFrontPageDataResponse> orderTraceLogsRecordItemsCommonPage =
                CommonPage.restPage(orderTraceLogsRecordItemsService.getListH5(request, pageParamRequest));
        return CommonResult.success(orderTraceLogsRecordItemsCommonPage);
    }

    /**
     * 新增订单溯源号段明细表
     * @param request 新增参数
     * @author Caiyc
     * @since 2025-08-04
     */
    @ApiOperation(value = "新增溯源日志")
    @RequestMapping(value = "/items/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated OrderTraceLogsRecordItemsFrontSaveRequest request) {
        if(orderTraceLogsRecordItemsService.create(request)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 批量删除
     * @param request
     * @return
     * @author Caiyc
     * @since 2025-08-04
     */
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.DELETE, description = "批量删除溯源套餐")
    @ApiOperation(value = "批量删除溯源日志")
    @RequestMapping(value = "/items/batch/delete", method = RequestMethod.POST)
    public CommonResult<String> batchDelete(@RequestBody @Validated CommonBatchRequest request) {
        if (orderTraceLogsRecordItemsService.batchDelete(request.getIdList())) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 修改订单溯源号段明细表
     * @param request 修改参数
     * @author Caiyc
     * @author Caiyc
     * @since 2025-08-04
     */
    @ApiOperation(value = "修改溯源日志")
    @RequestMapping(value = "/items/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated OrderTraceLogsRecordItemsFrontSaveRequest request) {
        if (orderTraceLogsRecordItemsService.update(request)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询订单溯源号段明细表详情
     * @param id Integer
     * @author Caiyc
     * @since 2025-08-04
     */
    @ApiOperation(value = "溯源日志详情")
    @RequestMapping(value = "/items/info/{id}", method = RequestMethod.GET)
    public CommonResult<OrderTraceLogsRecordItemsDetailResponse> getDetails(@PathVariable("id") Integer id) {
        return CommonResult.success(orderTraceLogsRecordItemsService.getDetails(id));
    }

    @ApiOperation(value = "通过经纬度获取地址")
    @RequestMapping(value = "/cur/address", method = RequestMethod.GET)
    public CommonResult<AddressDetailResponse> getCurAddress(@RequestParam("longitude") String longitude, @RequestParam("latitude") String latitude) {
        return CommonResult.success(orderTraceLogsRecordItemsService.getCurAddress(longitude, latitude));
    }

    @ApiOperation(value = "检查溯源日志是否已填写")
    @RequestMapping(value = "/items/exists", method = RequestMethod.POST)
    public CommonResult<OrderTraceLogsRecordItemsExistsResponse> checkExists(@RequestBody @Validated OrderTraceLogsRecordItemsExistsRequest request) {
        return CommonResult.success(orderTraceLogsRecordItemsService.checkExists(request));
    }

}



